package com.haierp.service.authority.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.framework.service.impl.SuperServiceImpl;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.haierp.mapper.authority.UserMapperHai;
import com.haierp.mapper.authority.UserRoleMapperHai;
import com.haierp.model.authority.UserHai;
import com.haierp.model.authority.UserRoleHai;
import com.haierp.service.authority.IUserServiceHai;
import com.haierp.util.JsonPageResult;
import com.haierp.vo.authority.UserQueryVO;
import com.wangzhixuan.commons.utils.BeanUtils;

/**
 * 
 * Title: UserServiceImpl.java
 * Description: 
 *
 * @author jc
 * Apr 18, 2017
 *
 */
@Service("userServiceHai")
public class UserServiceHaiImpl extends SuperServiceImpl<UserMapperHai, UserHai> implements IUserServiceHai {

    @Autowired
    private UserMapperHai iUserMapperHai;
    
    @Autowired
    private UserRoleMapperHai iUserRoleMapperHai;

	@Override
	public Integer queryUsersCount(UserQueryVO userQueryVO)
	{
		return iUserMapperHai.queryUsersCount(userQueryVO);
	}
    
    @Override
    public List<UserHai> queryByLoginName(UserQueryVO userQueryVO) {
        UserHai user = new UserHai();
        user.setLoginName(userQueryVO.getLoginName());
        EntityWrapper<UserHai> wrapper = new EntityWrapper<UserHai>(user);
        if (null != userQueryVO.getId()) {
            wrapper.where("id != {0}", userQueryVO.getId());
        }
        return this.selectList(wrapper);
    }

    @Override
    public void insertByVo(UserQueryVO userQueryVO) {
        UserHai user = BeanUtils.copy(userQueryVO, UserHai.class);
        user.setCreateTime(new Date());
        this.insert(user);
        
        Long id = user.getId();
        String[] roles = userQueryVO.getRoleIds().split(",");
        UserRoleHai userRole = new UserRoleHai();
        for (String string : roles) {
            userRole.setUserId(id);
            userRole.setRoleId(Long.valueOf(string));
            iUserRoleMapperHai.insert(userRole);
        }
    }

    @Override
    public UserQueryVO queryVoById(Long id) {
        return iUserMapperHai.queryUserQueryVOById(id);
    }

    @Override
    public void updateByVo(UserQueryVO userQueryVO) {
        UserHai user = BeanUtils.copy(userQueryVO, UserHai.class);
        if (StringUtils.isBlank(user.getPassword())) {
            user.setPassword(null);
        }
        this.updateSelectiveById(user);
        
        Long id = userQueryVO.getId();
        List<UserRoleHai> userRoles = iUserRoleMapperHai.queryByUserId(id);
        if (userRoles != null && !userRoles.isEmpty()) {
            for (UserRoleHai userRole : userRoles) {
                iUserRoleMapperHai.deleteById(userRole.getId());
            }
        }

        String[] roles = userQueryVO.getRoleIds().split(",");
        UserRoleHai userRole = new UserRoleHai();
        for (String string : roles) {
            userRole.setUserId(id);
            userRole.setRoleId(Long.valueOf(string));
            iUserRoleMapperHai.insert(userRole);
        }
    }

    @Override
    public void updatePwdByUserId(Long userId, String md5Hex) {
        UserHai user = new UserHai();
        user.setId(userId);
        user.setPassword(md5Hex);
        this.updateSelectiveById(user);
    }

    @Override
    public void deleteUserById(Long id) {
        iUserMapperHai.deleteById(id);
        List<UserRoleHai> userRoles = iUserRoleMapperHai.queryByUserId(id);
        if (userRoles != null && !userRoles.isEmpty()) {
            for (UserRoleHai userRole : userRoles) {
                iUserRoleMapperHai.deleteById(userRole.getId());
            }
        }
    }

	@Override
	public JsonPageResult<List<UserQueryVO>> queryUserQueryVOList(UserQueryVO userQueryVO) {
		JsonPageResult<List<UserQueryVO>> userResult = new JsonPageResult<>();
		
		Integer totalCount = iUserMapperHai.queryUsersCount(userQueryVO);
		
		if ((null != totalCount) && (0L != totalCount)) {
			userResult.buildPage(totalCount, userQueryVO);
			
			List<UserQueryVO> users = iUserMapperHai.queryUserQueryVOList(userQueryVO);
			userResult.setData(users);			
		} else {
			List<UserQueryVO> users = new ArrayList<>();
			userResult.setData(users);
		}
		
		return userResult;
	}
    
	@Override
	public List<UserHai> queryBuyer() {
		Map<String, Object> columnMap = new HashMap<>();
		columnMap.put("organization_id", 7);
		return iUserMapperHai.selectByMap(columnMap);
	}

	@Override
	public List<UserHai> queryCreaterList() {
		String[] strArray={"CGB","YYB","01"};
		List<String> list = Arrays.asList(strArray);  
		List<UserHai> users = iUserMapperHai.queryCreaterList(list);
		return users;
	}

}